﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PlatformUW.Contracts;
using PlatformUW.Entities.Dtos;

namespace PlatformUW.API.Controllers
{
    [Route("api/character")]
    [ApiController]
    public class CharacterController : ControllerBase
    {
        private readonly IRepositoryWrapper repositoryWrapper;
        private readonly ILogger<PlayerController> logger;
        private readonly IMapper mapper;

        public CharacterController(IRepositoryWrapper repositoryWrapper,
            ILogger<PlayerController> logger,
            IMapper mapper)
        {
            this.repositoryWrapper = repositoryWrapper;
            this.logger = logger;
            this.mapper = mapper;
        }

        [HttpGet("all")]
        public async Task<IActionResult> GetAllPlayers()
        {
            try
            {
                var characters = await repositoryWrapper.Character.GetAllAsync();
                var result = mapper.Map<IEnumerable<CharacterDto>>(characters);
                return Ok(result);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message, "Alarm");
                return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while processing your request.");
            }
        }

        [HttpGet("{id}", Name = "characterId")]
        public IActionResult GetPlayerById(Guid id)
        {
            try
            {
                var character = repositoryWrapper.Character.GetCharactersByIDAsync(id);
                if (character == null)
                {
                    return NotFound();
                }
                var result = mapper.Map<CharacterDto>(character.Result);
                return Ok(result);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message, "Alarm");
                return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while processing your request.");
            }
        }

    }
}
